package com.rtx.wms.std.transer.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rtx.common.EdiBeanUtils;
import com.rtx.edi.model.api.bo.ApiMappingInfoBo;
import com.rtx.wms.std.ctclass.ClassHolder;
import com.rtx.wms.std.so.bean.ApiOrderdetail;
import com.rtx.wms.std.so.bean.ApiOrders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.transer.bean.ApiTransfer;
import com.rtx.wms.std.transer.bean.ApiTransferdetail;
import com.rtx.wms.std.transer.mapper.ApiTransferMapper;
import com.rtx.wms.std.transer.service.ApiTransferService;
import com.rtx.wms.std.transer.service.ApiTransferdetailService;

import cn.hutool.core.lang.UUID;

@Service
public class ApiTransferServiceImpl extends ServiceImpl<ApiTransferMapper, ApiTransfer> implements ApiTransferService{

	@Autowired
	ApiTransferdetailService apiTransferdetailService;
	
	@Transactional
	@DataSourceAnnotation
	@Override
	public void addTransfer(String datasourckey, ApiTransfer apiTransfer) {
		String batchid = UUID.randomUUID().toString();
		for(ApiTransferdetail detail:apiTransfer.getDetails()) {
			detail.setBatchid(batchid);
			this.baseMapper.rtxconfigColumnsSave(detail);
		}
		apiTransfer.setEdiflag("P");
		apiTransfer.setRtxqueue(0);
		apiTransfer.setBatchid(batchid);
		this.baseMapper.rtxconfigColumnsSave(apiTransfer);
	}

	@DataSourceAnnotation
	@Override
	public void queryByCondition(String datasourckey, Wrapper<ApiTransfer> query) {
		this.list(query);
	}

	@DataSourceAnnotation
	@Override
	public List<ApiTransfer> queryWithDetailByCondition(String datasourckey, Wrapper<ApiTransfer> query) {
		List<ApiTransfer> list = this.list(query);
		for(ApiTransfer transfer:list) {
			QueryWrapper<ApiTransferdetail> tdqw = Wrappers.query();
			tdqw.eq("batchid", transfer.getBatchid());
			List<ApiTransferdetail> tdlist =apiTransferdetailService.list(tdqw);
			transfer.setDetails(tdlist);
		}
		return list;
	}

	@DataSourceAnnotation
	@Override
	public List<ApiTransfer> queryWithDetailByCondition(String datasourckey, QueryWrapper<ApiTransfer> query, ApiMappingInfoBo bo) {
		List<ApiTransfer> list = convertToConfigApiTransfer(query,bo);
		for(ApiTransfer transfer:list) {
			QueryWrapper<ApiTransferdetail> tdqw = Wrappers.query();
			tdqw.eq("batchid", transfer.getBatchid());
			List<ApiTransferdetail> tdlist =convertToConfigApiTransferdetail(tdqw,bo);
			transfer.setDetails(tdlist);
		}
		return list;
	}

	List<ApiTransfer> convertToConfigApiTransfer(QueryWrapper<ApiTransfer> query, ApiMappingInfoBo bo){
		List<ApiTransfer> head = new ArrayList<>();
		try {
			String apiid =  bo.getTargetApi().getId()+"";
			Map<String,Class> map = ClassHolder.apiconfigClassMap.get(apiid);
			if(map != null && map.get("main") != null) {
				Class headclass = map.get("main");
				query.select(EdiBeanUtils.getCoulmns(headclass));
				List<Map<String, Object>> oriData = baseMapper.selectMaps(query);
				for(Map<String, Object> dataMap:oriData) {
					ApiTransfer o =(ApiTransfer) EdiBeanUtils.copyFields(dataMap, headclass);
					head.add(o);
				}
			}else {
				head = baseMapper.selectList(query);
			}
		} catch (Exception e) {
			head = baseMapper.selectList(query);
		}
		return head;
	}

	List<ApiTransferdetail> convertToConfigApiTransferdetail(QueryWrapper<ApiTransferdetail> query,ApiMappingInfoBo bo){
		List<ApiTransferdetail> head = new ArrayList<>();
		try {
			String apiid =  bo.getTargetApi().getId()+"";
			Map<String,Class> map = ClassHolder.apiconfigClassMap.get(apiid);

			if(map != null && map.get("sub") != null) {
				Class detailclass = map.get("sub");
				query.select(EdiBeanUtils.getCoulmns(detailclass));
				List<Map<String, Object>> oriData = apiTransferdetailService.listMaps(query);
				for(Map<String, Object> dataMap:oriData) {
					ApiTransferdetail o =(ApiTransferdetail) EdiBeanUtils.copyFields(dataMap, detailclass);
					head.add(o);
				}
			}else {
				head = apiTransferdetailService.list(query);
			}
		} catch (Exception e) {
			head = apiTransferdetailService.list(query);
		}

		return head;
	}

	@DataSourceAnnotation
	@Override
	public void updApiTransferStatus(String datasourckey, Wrapper<ApiTransfer> upd) {
		this.update(upd);
	}
	
	
}
